package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.DepositoBean;
import beans.DepositoCentralBean;

public class HibernateDepositoDAO {
	private static HibernateDepositoDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateDepositoDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateDepositoDAO();
		}
		return instancia;
	}

	public void grabarDeposito(DepositoBean m) {
		/* Abro sesion y grabo el objeto */
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public List<DepositoBean> getDepositos() {
		Session session = sf.openSession();
		List<DepositoBean> depositos = session.createQuery(
				"from DepositoBean m ").list();
		session.close();
		return depositos;

	}

	public void updateDeposito(DepositoBean m) {

		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(m);
		session.flush();
		session.getTransaction().commit();
		session.close();

	}

	public DepositoBean getDeposito(int nroDeposito) {

		Session session = sf.openSession();
		DepositoBean m;
		m = (DepositoBean) session.get(DepositoBean.class, nroDeposito);
		return m;

	}

	public DepositoBean buscarDeposito(DepositoBean movb) {
		Session session = sf.openSession();
		DepositoBean m;
		m = (DepositoBean) session
				.get(DepositoBean.class, movb.getIdDeposito());
		return m;
	}

	public DepositoCentralBean buscarDepositoCentral(DepositoCentralBean depb) {
		Session session = sf.openSession();
		DepositoCentralBean m = new DepositoCentralBean();
		m = (DepositoCentralBean) session.get(DepositoCentralBean.class,
				depb.getIdDeposito());
		return m;
	}

	public void updateDepositoCentral(DepositoCentralBean dep) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(dep);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void mergeDepositoCentral(DepositoCentralBean depcBean) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(depcBean);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void mergeDeposito(DepositoBean depBean) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(depBean);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
}
